﻿<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>


<table id="myDatagrid3"></table>

<div id="myToolbar3">
    <a href="javascript:void(0)" class="easyui-linkbutton"
       data-options="iconCls:'icon-add'" onclick="toOpenAddDialog()">添加</a>
    <a href="javascript:void(0)" class="easyui-linkbutton"
       data-options="iconCls:'icon-remove'" onclick="doDelete()">批量删除</a>
</div>

<div id="addDiv3">
    <form id="addForm3" method="post" enctype="multipart/form-data" >
        名字:<input id="addname" name="bannerOldName"/><br/>
        头像:
        <input id="addimage" src="${pageContext.request.contextPath}/img/6.gif" width="20px" height="20px" type="file" class="offset10 lf" name="tupian"/><br/>
        是否删除:
                <select id="addstate" name="bannerState" size="1" class="sel">
                    <option value="0">正常</option>
                    <option value="1">删除</option>
                </select><br/>
        是否展示:
                <select id="adddescription" name="bannerDescription" size="1" class="sel">
                    <option value="1">展示</option>
                    <option value="0">未展示</option>
                </select>
    </form>
</div>

<div id="addSubmitBtn3">
    <a href="javascript:void(0)" onclick="doadd()" class="easyui-linkbutton" data-options="iconCls:'icon-ok'">提交</a>
</div>

<div id="updateDiv3">
    <form id="updateForm3" method="post" enctype="multipart/form-data" >
        <input id="id" name="bannerId" type="hidden"/>
        名字:<input id="updatename" name="bannerOldName"/><br/>
        头像:
        <input id="updateimage" src="" width="20px" height="20px" type="file" class="offset10 lf" name="tupian"/><br/>
        是否删除:
                <select id="updatestate" name="bannerState" size="1" class="sel">
                    <option value="0">正常</option>
                    <option value="1">删除</option>
                </select><br/>
        是否展示:
                <select id="updatedescription" name="bannerDescription" size="1" class="sel">
                    <option value="1">展示</option>
                    <option value="0">不展示</option>
                </select>
    </form>
</div>
<!-- 修改对话框对应的HTML===END=== -->
<div id="updateSubmitBtn3">
    <a href="javascript:void(0)" onclick="doUpdate()" class="easyui-linkbutton" data-options="iconCls:'icon-ok'">提交</a>
</div>

<script>
    $(function(){
        $("#myDatagrid3").datagrid({
            url:"${pageContext.request.contextPath}/showBanners.do",
            pagination:true,
            toolbar:"#myToolbar3",
            columns:[[
                {field:'bannerId',title:'ID',width:100},
                {field:'bannerOldName',title:'名字',width:100},
                {field:'bannerImageUrl',title:'轮播图',width:300,formatter:imgFormatter},
                {field:'bannerState',title:'删除',width:100,formatter: function(value,row,index){
                        if (row.bannerState==0){
                            value="正常";
                            return value;
                        }
                        else {
                            value="已删除";
                            return value;
                        }}},

                {field:'bannerDescription',title:'是否展示',width:100,formatter: function(value,row,index){
                        if (row.bannerDescription==1){
                            value="展示";
                            return value;
                        } else{
                            value="未展示";
                            return value;
                        }}}
            ]],

            //双击
            onDblClickRow:function(rowIndex,rowData){
                //1.打开修改对话框
                $("#updateDiv3").dialog("open");
                //2.把数据写入到修改对话框中的对应位置
                $("#id").val(rowData.bannerId);
                $("#updatename").val(rowData.bannerOldName);
                $("#updateimage").prop("src","${pageContext.request.contextPath}"+rowData.bannerImageUrl);
                $("#updatestate").val(rowData.bannerState);

                $("#updatedescription").val(rowData.bannerDescription);


            }
        });
        function imgFormatter(value,row,index){
            if(value){
                return"<img src='${pageContext.request.contextPath}"+value+"'style='width:30px;height:30px'";
            }else{
                return null;}
        }
        $("#updateDiv3").dialog({
            title:"修改对话框",
            buttons:"#updateSubmitBtn3",
            closed:true,
            width:300
        });

        $("#addDiv3").dialog({
            title:"添加用户",
            buttons:"#addSubmitBtn3",
            closed:true,
            width:300
        });
    });


    function toOpenAddDialog(){
        $("#addDiv3").dialog("open");
    }

    function doadd(){
        $("#addForm3").form("submit",{
            url:"${pageContext.request.contextPath}/addBanner.do",

            success:function(data){

                var jsObj = JSON.parse(data);
                if(jsObj.isAdd){
                    $.messager.alert('提示框',"添加成功",'info');
                    $("#addDiv3").dialog("close");
                    $("#myDatagrid3").datagrid("reload");
                }else{
                    $.messager.alert('提示框',"添加失败",'warning');
                }
            }
        });
    }
    function doUpdate(){

        $("#updateForm3").form("submit",{
            url:"${pageContext.request.contextPath}/updateBanner.do",
            success:function(data){
                var jsObj = JSON.parse(data);
                if(jsObj.isUpdate){
                    $.messager.alert('提示框',"修改成功",'info');
                    $("#updateDiv3").dialog("close");
                    $("#myDatagrid3").datagrid("reload");
                }else{
                    $.messager.alert('提示框',"修改失败",'warning');
                }
            }
        });

    }


    function doDelete(){



        var allSelectedRows = $("#myDatagrid3").datagrid("getSelections");

        if(allSelectedRows.length==0){

            $.messager.alert('提示',"请选中后再删除",'warning');
        }else{
            $.messager.confirm('确认', '确认要删除吗?', function(r){
                if (r){
                    var id=new Array();
                    for(var i=0;i<allSelectedRows.length;i++){
                        id[i]=allSelectedRows[i].bannerId;
                    }
                    $.ajax({
                        data:{"id":id},
                        url:"${pageContext.request.contextPath}/deleteBanner.do",
                        type:"post",
                        traditional:true,
                        success:function(data){

                            $("#myDatagrid3").datagrid("reload");

                        }
                    });
                }
            });

        }

    }
</script>